﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class SystemSet : System.Windows.Forms.Form
	{
		public SystemSet(Categories groups, List<Element> list, List<Element> list3D, Document doc)
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
			this.list = list;
			this.list3D = list3D;
			this.groups = groups;
			this.doc = doc;
			base.CenterToScreen();
		}

		private XYZ getElementXYZ(Element ei)
		{
			XYZ result = null;
			if (ei.Location is LocationCurve)
			{
				if ((ei.Location as LocationCurve).Curve.GetEndPoint(0).Z > (ei.Location as LocationCurve).Curve.GetEndPoint(1).Z)
				{
					result = (ei.Location as LocationCurve).Curve.GetEndPoint(1);
				}
				else
				{
					result = (ei.Location as LocationCurve).Curve.GetEndPoint(0);
				}
			}
			if (ei.Location is LocationPoint)
			{
				result = (ei.Location as LocationPoint).Point;
			}
			return result;
		}

		private bool haveSystemClassification(Element elem, string systemClass)
		{
			bool result = false;
			if (elem.get_Parameter(BuiltInParameter.RBS_SYSTEM_CLASSIFICATION_PARAM) != null)
			{
				string text = elem.get_Parameter(BuiltInParameter.RBS_SYSTEM_CLASSIFICATION_PARAM).AsString();
				if (text.Contains(","))
				{
					foreach (string a in text.Split(new char[]
					{
						','
					}))
					{
						foreach (string b in systemClass.Split(new char[]
						{
							','
						}))
						{
							if (a == b)
							{
								result = true;
								break;
							}
						}
					}
				}
				else
				{
					foreach (string b2 in systemClass.Split(new char[]
					{
						','
					}))
					{
						if (text == b2)
						{
							result = true;
							break;
						}
					}
				}
			}
			return result;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (this.checkBoxArch.Checked)
			{
				this.categoryHide("Archtecture");
			}
			if (this.checkBoxMepDuct.Checked)
			{
				this.categoryHide("DuctSystem");
			}
			if (this.checkBoxMepPipe.Checked)
			{
				this.categoryHide("PipeSystem");
			}
			if (this.checkBoxFire.Checked)
			{
				this.categoryHide("FireSystem");
			}
			if (this.checkBoxWater.Checked)
			{
				this.categoryHide("WaterSystem");
			}
			this.ElementHide();
			if (!this.checkBoxArch.Checked && !this.checkBoxMepDuct.Checked && !this.checkBoxMepPipe.Checked && !this.checkBoxFire.Checked && !this.checkBoxWater.Checked)
			{
				YJKMessageBox.Show("请至少选择一个专业！");
			}
		}

		private void categoryHide(string nodeName)
		{
			foreach (SystemSetStruct systemSetStruct in SystemSetHelper.read(nodeName, SystemSet.path))
			{
				List<Element> elementByCategory = YJKElementFilter.GetElementByCategory(this.doc, systemSetStruct.CategoryName);
				if (elementByCategory.Count != 0)
				{
					foreach (Element element in elementByCategory)
					{
						if (this.haveSystemClassification(element, SystemSetHelper.readSystemClassAndLevelInfo(nodeName).ElementAt(0)) && SystemSetHelper.isHaveInTheLevel(this.getElementXYZ(element), SystemSetHelper.readSystemClassAndLevelInfo(nodeName).ElementAt(1), SystemSetHelper.readSystemClassAndLevelInfo(nodeName).ElementAt(2)))
						{
							this.elementList.Add(element.Id);
						}
						else if (element is Wall || element is Ceiling || element is Floor || element.Category.Id.IntegerValue == -2000023 || element.Category.Id.IntegerValue == -2000014 || element.Category.Id.IntegerValue == -2000100)
						{
							this.elementList.Add(element.Id);
						}
					}
				}
			}
		}

		private void ElementHide()
		{
			if (this.elementList.Count > 0)
			{
				foreach (Element element in this.list)
				{
                    Autodesk.Revit.DB.View view = (Autodesk.Revit.DB.View)element;
					view.HideElements(SystemSetHelper.getALLELement(this.doc, SystemSet.path));
					view.UnhideElements(this.elementList);
				}
				foreach (Element element2 in this.list3D)
				{
                    Autodesk.Revit.DB.View view2 = (Autodesk.Revit.DB.View)element2;
					view2.HideElements(SystemSetHelper.getALLELement(this.doc, SystemSet.path));
					view2.UnhideElements(this.elementList);
				}
				base.Close();
				return;
			}
			YJKMessageBox.Show("要显示的类别中的元素为空");
		}

		private void button1_Click(object sender, EventArgs e)
		{
            System.Windows.Forms.Form form = new FormSystemSetDetailed(this.doc);
			form.ShowDialog();
		}

		private void button3_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private List<Element> list = new List<Element>();

		private List<Element> list3D = new List<Element>();

		private Categories groups;

		private Document doc;

		private List<ElementId> elementList = new List<ElementId>();

		public static string path = Path.Combine(Product.DataLocation, "User\\systemSetInfo.xml");
	}
}
